//
//  RMNoteTableView.m
//  RMNote
//
//  Created by lxf on 16/4/18.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMNoteTableView.h"
#import "RMNoteCell.h"
#import "RMTimeHandle.h"
#import "RMNoteHeaderView.h"
#import "RMDateModel.h"
#import "RMDateBaseManager.h"

#define kRMNoteCell @"RMNoteCell"
@interface RMNoteTableView ()<UITableViewDataSource,UITableViewDelegate>{
    NSInteger _sectionNum;
}
// 年的数组 按照最近日期排序
@property (nonatomic, strong) NSArray *years;
@property (nonatomic, strong) RMDateModel *dateModel;
// 月的数组 按照最进日期排序
@property (nonatomic, strong) NSMutableArray *monthArr;
// 月所对应的日的数组 [[1,2,3],[2,3,4]]
@property (nonatomic, strong) NSMutableArray *month_DayArr;
//@property (nonatomic, strong) NSMutableArray *keysArr;



@end

@implementation RMNoteTableView



- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style byYear:(NSString *)year byMonth:(NSString *)month{
    if (self = [super initWithFrame:frame style:style]) {
        [self registerNib:[UINib nibWithNibName:@"RMNoteCell" bundle:nil] forCellReuseIdentifier:kRMNoteCell];
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
   
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style model:(RMDateModel *)model{
    if (self = [super initWithFrame:frame style:style]) {
       [self registerNib:[UINib nibWithNibName:@"RMNoteCell" bundle:nil] forCellReuseIdentifier:kRMNoteCell];
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dateModel = model;
        self.years = self.dateModel.years;
        self.monthArr = model.monthArr;
        self.month_DayArr = model.month_DayArr;
        self.month_yearArr = model.month_yearArr;

        [[NSNotificationCenter defaultCenter]addObserverForName:@"change" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            NSString *indexStr = note.object;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:indexStr.intValue];
            [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }];
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}

/*
- (void)getMonthsAndTimeArr{
    for (NSString *year in self.years) {
        NSArray *months = [self.dateModel monthsForYear:year];
        [self.monthArr addObjectsFromArray:months];
//        [self.monthArr addObject:months];
        for (NSString *month in months) {
            
            NSString *month_year = [NSString stringWithFormat:@"%@-%@",month,year];
            [self.month_yearArr addObject:month_year];
            
            NSMutableArray *tmpDayArr = [[RMDateBaseManager defaultManager]selectNotebySQLStr:[NSString stringWithFormat:@"select RMModel from %@ where year = %@ and month = %@",kRMNoteTable,year,month]].mutableCopy;
            [tmpDayArr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
                RMBaseModel *model1 = (RMBaseModel *)obj1;
                RMBaseModel *model2 = (RMBaseModel *)obj2;
                if ([model1.createDate compare:model2.createDate]==NSOrderedAscending) {
                    return NSOrderedDescending;
                }else if([model1.createDate compare:model2.createDate]==NSOrderedDescending){
                    return NSOrderedAscending;
                }else{
                    return NSOrderedSame;
                }
            }];
            [self.month_DayArr addObject:tmpDayArr];
            
        }
        
    }
    
}
*/

#pragma mark - 懒加载
- (NSMutableArray *)monthArr{
    if (!_monthArr) {
        _monthArr = [NSMutableArray array];
    }
    return _monthArr;
}

- (NSMutableArray *)month_DayArr{
    if (!_month_DayArr) {
        _month_DayArr = [NSMutableArray array];
    }
    return _month_DayArr;
}

- (NSMutableArray *)month_yearArr{
    if (!_month_yearArr) {
        _month_yearArr = [NSMutableArray array];
    }
    return _month_yearArr;
}


#pragma mark -UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.monthArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *dayArr = self.month_DayArr[section];
    return dayArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RMNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:kRMNoteCell forIndexPath:indexPath];
    cell.model = self.month_DayArr[indexPath.section][indexPath.row];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RMNoteCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    self.selectCellBlock(cell.model);

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    
    RMNoteHeaderView *view = [[[NSBundle mainBundle]loadNibNamed:@"RMNoteHeaderView" owner:nil options:nil] lastObject];
    view.month = self.monthArr[section];

    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 158;
    

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSArray *arr = [self visibleCells];
    
    if (arr.count>0) {
        RMNoteCell *cell = arr[0];
        NSIndexPath *index = [self indexPathForCell:cell];
        
//        NSLog(@"%ld",index.section);
        if (_sectionNum != index.section) {
            // 通知 datecell 刷新
            
            _sectionNum = index.section;
            NSString *sectionNum = [NSString stringWithFormat:@"%ld",(long)_sectionNum];
            NSDate *date = cell.model.createDate;
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"sectionChange" object:date];
            
            
            NSLog(@"%ld",(long)_sectionNum);
        }
        
        
    }
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"change" object:nil];
}



@end
